package com.lxr0860.lxr0860production_test.itemactivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.TextView;


import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.view.CustomDialog;
import com.lxr0860.lxr0860production_test.view.HorizontalProgressBar;

import org.jetbrains.annotations.Nullable;


/**
 * Created by uniriho on 2018/9/3.
 */

public class AccelerometerActivity extends Activity {
    private TextView mTvNumberX, mTvNumberY, mTvNumberZ;
    private HorizontalProgressBar mPbNumberX, mPbNumberY, mPbNumberZ;
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private SensorEventListener mListener;
    private static final int MAX_ACCELEROMETER=19460;
    private static final int MIN_ACCELEROMETER=0;
    private boolean maxX,maxY,maxZ;
    private CustomDialog.Builder builder;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent=new Intent();
                    Bundle bundle=new Bundle();
                    bundle.putBoolean("result",true);
                    mIntent.putExtras(bundle);
                    AccelerometerActivity.this.setResult(Contants.TEST_ITEM_ACCELEROMETER,mIntent);
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    builder=null;
                    maxX=maxY=maxZ=false;
                    break;
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accelerometer);
        initView();
        initData();
    }


    private void initView() {
        mTvNumberX = (TextView) findViewById(R.id.tv_accelerometer_x);
        mTvNumberY = (TextView) findViewById(R.id.tv_accelerometer_y);
        mTvNumberZ = (TextView) findViewById(R.id.tv_accelerometer_z);
        mPbNumberX = findViewById(R.id.pb_accelerometer_x);
        mPbNumberY = findViewById(R.id.pb_accelerometer_y);
        mPbNumberZ =  findViewById(R.id.pb_accelerometer_z);
    }

    private void initData() {
        mPbNumberX.setMax(MAX_ACCELEROMETER);
        mPbNumberY.setMax(MAX_ACCELEROMETER);
        mPbNumberZ.setMax(MAX_ACCELEROMETER);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                int x = (int) (event.values[SensorManager.DATA_X] * 1000);
                int y = (int) (event.values[SensorManager.DATA_Y] * 1000);
                int z = (int) (event.values[SensorManager.DATA_Z] * 1000);
                if(x==MAX_ACCELEROMETER){
                    maxX=true;
                }
                if(y==MAX_ACCELEROMETER){
                    maxY=true;
                }
                if (z==MAX_ACCELEROMETER){
                    maxZ=true;
                }
                mTvNumberX.setText("X: " + x);
                mTvNumberY.setText("Y: " + y);
                mTvNumberZ.setText("Z: " + z);
                mPbNumberX.setProgress(Math.abs(x));
                mPbNumberY.setProgress(Math.abs(y));
                mPbNumberZ.setProgress(Math.abs(z));
                if(maxX&&maxY&&maxZ){
                    if(builder==null){
                        builder= Contants.showDailog(
                                AccelerometerActivity.this,"重力感应正常，是否保存测试",handler, Gravity.TOP);
                    }
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };
        mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onDestroy() {
        super.onDestroy();
        //注销监听器
        if (mListener != null) {
            mSensorManager.unregisterListener(mListener);
        }
    }
}
